package com.lf.util;

import java.sql.ResultSet;
import java.util.regex.Pattern;

import com.lf.connect.Connect;
import com.lf.connect.Mysql;

public class Common {

	public static boolean Check(String pat, String str) {
		boolean rs = false;
		try {
			if (str != null)
				rs = Pattern.matches(pat, str);
		} catch (Exception e) {
		}
		return rs;
	}

	public static boolean checkUsername(String username) {
		boolean rs = false;
		try {
			if (username != null)
				rs = Pattern.matches("[\\w\\-]{1,32}", username);
		} catch (Exception e) {
		}
		return rs;
	}

	public static boolean checkMD5(String md5) {
		boolean rs = false;
		try {
			if (md5 != null)
				rs = Pattern.matches("[\\w]{32}", md5);
		} catch (Exception e) {
		}
		return rs;
	}

	public static boolean checkSsid(String ssid) {
		boolean rs = false;
		try {
			if (ssid != null)
				rs = Pattern.matches("[\\w]{32}", ssid);
		} catch (Exception e) {
		}
		return rs;
	}

	public static boolean checkWlanacname(String wlanacname) {
		boolean rs = false;
		try {
			if (wlanacname != null)
				rs = Pattern.matches("[\\w\\-]{1,32}", wlanacname);
		} catch (Exception e) {
		}
		return rs;
	}

	public static boolean checkKey(String key) {
		boolean rs = false;
		try {
			if (key != null)
				rs = Pattern.matches("[\\w]+", key);
		} catch (Exception e) {
		}
		return rs;
	}

	public static boolean checkIPv4(String ip) {
		boolean rs = false;
		try {
			if (ip != null)
				rs = Pattern.matches(
						"[\\d]{1,3}.[\\d]{1,3}.[\\d]{1,3}.[\\d]{1,3}+", ip);
		} catch (Exception e) {
		}
		return rs;
	}

	public static boolean checkMac(String mac) {
		boolean rs = false;
		try {
			if (mac != null)
				rs = Pattern
						.matches(
								"[\\da-fA-F]{2}:[\\da-fA-F]{2}:[\\da-fA-F]{2}:[\\da-fA-F]{2}:[\\da-fA-F]{2}:[\\da-fA-F]{2}",
								mac);
		} catch (Exception e) {
		}
		return rs;
	}

	public static String getInfo(String model, String sql, String other) {
		StringBuilder backstr = new StringBuilder("");
		String strtmp = "";
		Connect con = new Mysql();
		ResultSet rs = null;
		rs = con.Query(sql);
		try {
			if (rs.next()) {
				int i = 0, rn = 0;
				rn = rs.getMetaData().getColumnCount();
				do {
					strtmp = model;
					for (i = 0; i < rn; i++) {
						strtmp = strtmp.replaceAll("\\$\\$" + i,
								rs.getString(i + 1) + "");
					}
					backstr.append(strtmp);
					strtmp = null;
				} while (rs.next());
			} else {
				backstr.append(other);
			}
		} catch (Exception e) {
			backstr.append("服务器内部出错！");
		}
		con.close();
		return backstr.toString();
	}
}
